﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;

public class NameTag
{
    public GUIModule Module;
    public IAvatarHandle Handle;
    private Rect nameTagPos = new Rect(0, 0, 0, 0);
    private Vector3 screenPos = Vector3.zero;
    public NameTag(GUIModule module)
    {
        Module = module;

        AppInterface.AvatarModule.OnIPlayerLogin += new Action<IAvatarHandle>(AvatarInterface_OnIPlayerLogin);
        AppInterface.AvatarModule.OnIPlayerLogout += new Action<IAvatarHandle>(AvatarInterface_OnIPlayerLogout);
        AppInterface.MonsterModule.OnAddMonster += new Action<IUnit>(addMonster);
        AppInterface.MonsterModule.OnDeleteMonster += new Action<IUnit>(deleteMonster);
    }
    private List<AvatarNameTagItem> avatarList = new List<AvatarNameTagItem>();
    private void AvatarInterface_OnIPlayerLogin(IAvatarHandle iAvatar)
    {
        avatarList.Add(new AvatarNameTagItem(iAvatar));
    }
    private void AvatarInterface_OnIPlayerLogout(IAvatarHandle iAvatar)
    {
        int index = -1;
        for (int i = 0; i < avatarList.Count; i++)
        {
            if (avatarList[i].Handle.Id == iAvatar.Id)
            {
                index = i;
            }
        }
        if (index >= 0) avatarList.RemoveAt(index);
    }
    private List<MonsterTagItem> monsterList = new List<MonsterTagItem>();
    private void addMonster(IUnit iMonster)
    {
        monsterList.Add(new MonsterTagItem(iMonster));
    }
    private void deleteMonster(IUnit iMonster)
    {
        int index = -1;
        for (int i = 0; i < monsterList.Count; i++)
        {
            if (monsterList[i].Handle.Id == iMonster.Id)
            {
                index = i;
            }
        }
        if (index >= 0) monsterList.RemoveAt(index);
    }

    private void AvatarInterface_OnIPlayerLogout(IUnit iMonster)
    {
        int index = -1;
        for (int i = 0; i < monsterList.Count; i++)
        {
            if (monsterList[i].Handle.Id == iMonster.Id)
            {
                index = i;
            }
        }
        if (index >= 0) monsterList.RemoveAt(index);
    }
    public void OnGUI()
    {
        for (int i = 0; i < avatarList.Count; i++)
        {
            avatarList[i].OnGUI();
        }
        for (int i = 0; i < monsterList.Count; i++)
        {
            monsterList[i].OnGUI();
        }
    }
    public void Update()
    {
        for (int i = 0; i < avatarList.Count; i++)
        {
            avatarList[i].Update();
        }
        for (int i = 0; i < monsterList.Count; i++)
        {
            monsterList[i].Update();
        }
    }
    public void FixedUpdate()
    {
        for (int i = 0; i < monsterList.Count; i++)
        {
            monsterList[i].FixedUpdate();
        }
    }
}
public class AvatarNameTagItem
{
    public IAvatarHandle Handle;
    private Rect nameTagPos = new Rect(0, 0, 0, 0);
    private Vector3 screenPos = Vector3.zero;
    public AvatarNameTagItem(IAvatarHandle handle)
    {
        Handle = handle;
        Vector2 size = AssetHelp.GetFontWidth(GuiStyles.NameTag, Handle.Name);
        nameTagPos.width = size.x;
        nameTagPos.height = size.y;
    }
    public void Update()
    {
        screenPos = Vector3.Lerp(screenPos, Define.MainCamera.WorldToScreenPoint(Handle.NameTagTransform.position), .33f);

        nameTagPos.x = screenPos.x - (nameTagPos.width / 2);
        nameTagPos.y = Screen.height - screenPos.y - (nameTagPos.height / 2);
        //nameTagPos = Vector2.Lerp(nameTagPos, Define.MainCamera.WorldToScreenPoint(transform.position) transform.position, 0333f);
    }
    public void OnGUI()
    {
        GUI.Label(nameTagPos, Handle.Name, GuiStyles.NameTag);
    }
}
public class MonsterTagItem
{
    public IUnit Handle;
    public static Texture MonsterHpBack;
    public static Texture MonsterHp;
    public static Vector2 HpSizeBack = new Vector2(64, 6);
    public static Vector2 HpSize = new Vector2(64, 6);

    public float HP;
    private Rect nameTagPosBack = new Rect(0, 0, 0, 0);
    private Rect nameTagPos = new Rect(0, 0, 0, 0);
    private Vector3 screenPos = Vector3.zero;
    private bool draw = true;
    public MonsterTagItem(IUnit handle)
    {
        Handle = handle;
        if (MonsterHpBack == null) MonsterHpBack = Resources.Load("GUI/enemy_HP_01") as Texture;
        if (MonsterHp == null) MonsterHp = Resources.Load("GUI/enemy_HP_02") as Texture;
        HP = 1.0f;
        handle.OnHpChange += new Action<IUnit, int>(handle_OnHpChange);
    }
    private void handle_OnHpChange(IUnit _monster, int _hp)
    {
        HP = _monster.Hp / (_monster.AllHp * 1.0f);
    }
    public void FixedUpdate()
    {
        draw = screenPos.x > 0 && screenPos.x < AppInterface.GUIModule.ScreenWidth && screenPos.y > 0 && screenPos.y < AppInterface.GUIModule.ScreenHeight;
    }
    public void Update()
    {
        if (Handle.Dead)
        {
            draw = false;
            return;
        }
        screenPos = Vector3.Lerp(screenPos, Define.MainCamera.WorldToScreenPoint(Handle.NameTagTransform.position), .33f);
        if (draw)
        {
            nameTagPosBack.width = HpSizeBack.x * AppInterface.GUIModule.WidthScale;
            nameTagPosBack.height = HpSizeBack.y * AppInterface.GUIModule.HeightScale;
            nameTagPosBack.x = screenPos.x - (HpSizeBack.x * AppInterface.GUIModule.WidthScale / 2.0f);
            nameTagPosBack.y = AppInterface.GUIModule.ScreenHeight - screenPos.y - (HpSizeBack.y * AppInterface.GUIModule.HeightScale / 2.0f);

            nameTagPos.width = HpSize.x * AppInterface.GUIModule.WidthScale * HP;
            nameTagPos.height = HpSize.y * AppInterface.GUIModule.HeightScale;
            nameTagPos.x = nameTagPosBack.x;
            nameTagPos.y = nameTagPosBack.y;
        }
    }
    public void OnGUI()
    {
        if (draw)
        {
            GUI.DrawTexture(nameTagPos, MonsterHp);
            GUI.DrawTexture(nameTagPosBack, MonsterHpBack);
        }
    }
}